<?php

namespace AdminBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use AdminBundle\Entity\AuthUser;

class DefaultController extends BaseController
{
    /**
     * admin default page
     *
     * @Route(path="/",name="bg_homepage")
     */
    public function indexAction(Request $request)
    {
        return $this->renderWithRole($request, 'AdminBundle:Default:index.html.twig');

        try {
            $this->denyAccessUnlessGranted('ROLE_USER', null, '拒绝访问');//拒绝访问
        } catch (\Exception $e) {
            return $this->redirectToRoute('authuser_loginfailed');
        }
        return $this->renderWithRole('AdminBundle:Default:index.html.twig');
    }

    /**
     * authUser check .
     *
     * @Route(path="/login_check", name="authuser_check")
     * @Method({"GET","POST"})
     */
    public function loginCheckAction(Request $request)
    {
        return $this->redirectToRoute('authuser_login');
    }

    /**
     * authUser check .
     *
     * @Route(path="/login_failed", name="authuser_loginfailed")
     * @Method({"GET","POST"})
     */
    public function loginFailedAction(Request $request)
    {
        $authenticationUtils = $this->get('security.authentication_utils');

        $error = $authenticationUtils->getLastAuthenticationError();

        return $this->render(
            'AdminBundle:Default:failed.html.twig', ['error' => $error]
        );
        return $this->loginAction($request);
    }

    /**
     * authUser check .
     *
     * @Route(path="/finish", name="authuser_logout")
     * @Method({"GET","POST"})
     */
    public function loginFinishAction(Request $request)
    {
        return $this->render(
            'AdminBundle:Default:finish.html.twig'
        );
    }

    /**
     * authUser login .
     *
     * @Route(path="/login", name="authuser_login")
     * @Method({"GET","POST"})
     */
    public function loginAction(Request $request)
    {
        $authenticationUtils = $this->get('security.authentication_utils');

        $error = $authenticationUtils->getLastAuthenticationError();

        $lastUsername = $authenticationUtils->getLastUsername();

        return $this->render(
            'AdminBundle:Default:login.html.twig',
            array(
                'last_username' => $lastUsername,
                'error' => $error
            )
        );
    }
}
